/*
 * @Author: 老刘
 * @Date: 2020-04-29 15:51:38
 * @Description: Swiper
 */
Page({
	data: {
		swiperList: [
			{url: '1'},
			{url: '2'},
			{url: '3'},
			{url: '4'}
		],
		swiperList1: [
			{
				title: '星期一',
				list: [
					{content: '7:00-7:30洗漱'},
					{content: '7:30-8:00去锻炼'},
					{content: '8:00-8:30吃早餐'},
					{content: '8:30-12:00去上早班'},
					{content: '12:00-12:30吃午饭'},
					{content: '12:30-13:30午休'},
					{content: '13:30-17:30去上下午班'},
					{content: '17:30-18:00吃晚饭'},
					{content: '18:00-21:00加班'},
					{content: '21:00-22:00去锻炼'},
					{content: '22:00-22:30洗澡'},
					{content: '22:30-23:30学习'},
					{content: '23:30-7:00睡觉'}
				]
			},
			{
				title: '星期二',
				list: [
					{content: '7:00-7:30洗漱'},
					{content: '7:30-8:00去锻炼'},
					{content: '8:00-8:30吃早餐'},
					{content: '22:30-23:30学习'},
					{content: '23:30-7:00睡觉'}
				]
			},
			{
				title: '星期三',
				list: [
					{content: '7:00-7:30洗漱'},
					{content: '7:30-8:00去锻炼'},
					{content: '8:00-8:30吃早餐'},
					{content: '8:30-12:00去上早班'},
					{content: '12:00-12:30吃午饭'},
					{content: '12:30-13:30午休'},
					{content: '13:30-17:30去上下午班'},
					{content: '17:30-18:00吃晚饭'},
					{content: '18:00-21:00加班'},
					{content: '21:00-22:00去锻炼'},
					{content: '22:00-22:30洗澡'},
					{content: '22:30-23:30学习'},
					{content: '23:30-7:00睡觉'}
				]
			},
			{
				title: '星期四',
				list: [
					{content: '7:00-7:30洗漱'},
					{content: '7:30-8:00去锻炼'},
					{content: '8:00-8:30吃早餐'},
					{content: '22:30-23:30学习'},
					{content: '23:30-7:00睡觉'}
				]
			},
			{
				title: '星期五',
				list: [
					{content: '7:00-7:30洗漱'}
				]
			}
		],
		swiperList2: [
			{url: '1'},
			{url: '2'},
			{url: '3'},
			{url: '4'},
			{url: '1'},
			{url: '2'}
		],
		swiperIndex: 0,
		cardCurrentIndex: 0,
		cardCurrentIndex1: 0,
		cardCurrentIndex2: 0,
		swiperHeight: 0,
		touchStart: 0,// X轴滑动位置
		direction: 'right',// 滑动方向
		isDotIndicator: false
	},
	onLoad () {
		this.getSwiperHeight();
		this.towerSwiper();
	},
	swiperChange (e) {
		this.setData({
			cardCurrentIndex: e.detail.current
		});
	},
	swiperChange2 (e) {
		this.setData({
			cardCurrentIndex2: e.detail.current
		});
	},
	swiperChange3 (e) {
		this.setData({
			swiperIndex: e.detail.current
		});
	},
	getSwiperHeight () {
		const swiperHeight = [];
		const that = this;
		that.data.swiperList1.map((item, index) => {
			console.log(item);
			const height = wx.createSelectorQuery();
			height.select(`.item-${index}`).boundingClientRect();
			height.exec((res) => {
				swiperHeight.push(res[0].height);
				if (swiperHeight.length === that.data.swiperList1.length) {
					that.setData({swiperHeight});
				}
			});
		});
	},
	// towerSwiper
	// 初始化towerSwiper
	towerSwiper () {
		const list = this.data.swiperList2;
		for (let i = 0; i < list.length; i++) {
			const num = parseInt((list.length / 2).toString(), (list.length / 2) + 1);
			list[i].zIndex = num + 1 - Math.abs(i - num);
			list[i].mLeft = i - num;
		}
		this.setData({
			swiperList2: list
		});
	},
	// towerSwiper触摸开始
	towerStart (e) {
		this.setData({
			touchStart: +e.touches[0].pageX
		});
	},
	// towerSwiper计算方向
	towerMove (e) {
		this.setData({
			direction: e.touches[0].pageX - (this.data.touchStart || 0) > 0 ? 'right' : 'left'
		});
	},
	// towerSwiper计算滚动
	towerEnd () {
		const direction = this.data.direction;
		const list = this.data.swiperList2;
		if (direction === 'right') {
			const mLeft = list[0].mLeft;
			const zIndex = list[0].zIndex;
			for (let i = 1; i < list.length; i++) {
				list[i - 1].mLeft = list[i].mLeft;
				list[i - 1].zIndex = list[i].zIndex;
			}
			list[list.length - 1].mLeft = mLeft;
			list[list.length - 1].zIndex = zIndex;
			this.setData({
				swiperList2: list
			});
		} else {
			const mLeft = list[list.length - 1].mLeft;
			const zIndex = list[list.length - 1].zIndex;
			for (let i = list.length - 1; i > 0; i--) {
				list[i].mLeft = list[i - 1].mLeft;
				list[i].zIndex = list[i - 1].zIndex;
			}
			list[0].mLeft = mLeft;
			list[0].zIndex = zIndex;
			this.setData({
				swiperList2: list
			});
		}
	}
});
